{% extends "base.html" %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <h2>劳育素质管理</h2>
        
        {% if current_user.role == 'student' %}
            <div class="card mt-4">
                <div class="card-header">
                    <h4>提交劳育素质评分</h4>
                </div>
                <div class="card-body">
                    <form method="POST" action="{{ url_for('submit_labor') }}" enctype="multipart/form-data">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                        
                        <div class="mb-3">
                            <label class="form-label">学期</label>
                            <select class="form-select" name="semester" required>
                                <option value="">请选择学期</option>
                                <option value="2023-2024">2023-2024</option>
                                <option value="2024-2025">2024-2025</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="labor_type">劳育类型</label>
                            <select class="form-select" name="labor_type" id="labor_type" required onchange="updateScore()">
                                <option value="">请选择劳育类型</option>
                                <option value="第一学年满16学时">第一学年满16学时</option>
                                <option value="第一学年达6-15学时">第一学年达6-15学时</option>
                                <option value="第一学年低于6学时">第一学年低于6学时</option>
                                <option value="第二（三）学年满32学时">第二（三）学年满32学时</option>
                                <option value="第二（三）学年未满32学时">第二（三）学年未满32学时</option>
                                <option value="入伍免修">入伍免修</option>
                            </select>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">劳育成绩</label>
                            <input type="number" class="form-control" name="labor_score" id="labor_score" readonly>
                            <div class="form-text" id="scoreHint"></div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">证明材料（选填）</label>
                            <input type="file" class="form-control" name="proof_file" accept=".pdf,.jpg,.jpeg,.png">
                            <div class="form-text">支持PDF、JPG、PNG格式，大小不超过5MB</div>
                        </div>

                        <button type="submit" class="btn btn-primary">提交评分</button>
                    </form>
                </div>
            </div>

            <!-- 显示历史记录 -->
            <div class="card mt-4">
                <div class="card-header">
                    <h4>我的劳育素质记录</h4>
                </div>
                <div class="card-body">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>学期</th>
                                <th>类型</th>
                                <th>得分</th>
                                <th>提交时间</th>
                                <th>状态</th>
                                <th>附件</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in labor_records %}
                            <tr>
                                <td>{{ record.semester }}</td>
                                <td>{{ record.labor_type }}</td>
                                <td>{{ record.labor_score }}</td>
                                <td>{{ record.submit_time.strftime('%Y-%m-%d %H:%M') }}</td>
                                <td>
                                    {% if record.status == 'pending' %}
                                        <span class="badge bg-warning">待审核</span>
                                    {% elif record.status == 'approved' %}
                                        <span class="badge bg-success">已通过</span>
                                    {% else %}
                                        <span class="badge bg-danger">已驳回</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if record.proof_file %}
                                        <a href="{{ url_for('download_labor_proof', file_id=record.id) }}" class="btn btn-sm btn-secondary">
                                            查看附件
                                        </a>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        {% else %}
            <!-- 教师查看界面 -->
            <div class="card mt-4">
                <div class="card-header">
                    <h4>待审核劳育素质</h4>
                </div>
                <div class="card-body">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>学生</th>
                                <th>学期</th>
                                <th>类型</th>
                                <th>成绩</th>
                                <th>提交时间</th>
                                <th>状态</th>
                                <th>附件</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in labor_records %}
                            {% if record.status == 'pending' %}
                            <tr>
                                <td>{{ record.student.username }} - {{ record.student.real_name }}</td>
                                <td>{{ record.semester }}</td>
                                <td>{{ record.labor_type }}</td>
                                <td>{{ record.labor_score }}</td>
                                <td>{{ record.submit_time.strftime('%Y-%m-%d %H:%M') }}</td>
                                <td>
                                    <span class="badge bg-warning">待审核</span>
                                </td>
                                <td>
                                    {% if record.proof_file %}
                                        <a href="{{ url_for('download_labor_proof', file_id=record.id) }}" class="btn btn-sm btn-secondary">
                                            查看附件
                                        </a>
                                    {% endif %}
                                </td>
                                <td>
                                    <button type="button" class="btn btn-sm btn-success" onclick="approveScore('labor', {{ record.id }})">通过</button>
                                    <button type="button" class="btn btn-sm btn-danger" onclick="rejectScore('labor', {{ record.id }})">驳回</button>
                                </td>
                            </tr>
                            {% endif %}
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- 已审核成绩表格 -->
            <div class="card mt-4">
                <div class="card-header">
                    <h4>已审核劳育素质</h4>
                </div>
                <div class="card-body">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>学生</th>
                                <th>学期</th>
                                <th>类型</th>
                                <th>成绩</th>
                                <th>提交时间</th>
                                <th>审核时间</th>
                                <th>状态</th>
                                <th>附件</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in labor_records %}
                            {% if record.status != 'pending' %}
                            <tr>
                                <td>{{ record.student.username }} - {{ record.student.real_name }}</td>
                                <td>{{ record.semester }}</td>
                                <td>{{ record.labor_type }}</td>
                                <td>{{ record.labor_score }}</td>
                                <td>{{ record.submit_time.strftime('%Y-%m-%d %H:%M') }}</td>
                                <td>{{ record.update_time.strftime('%Y-%m-%d %H:%M') if record.update_time else '' }}</td>
                                <td>
                                    {% if record.status == 'approved' %}
                                        <span class="badge bg-success">已通过</span>
                                    {% else %}
                                        <span class="badge bg-danger">已驳回</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if record.proof_file %}
                                        <a href="{{ url_for('download_labor_proof', file_id=record.id) }}" class="btn btn-sm btn-secondary">
                                            查看附件
                                        </a>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endif %}
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        {% endif %}
    </div>
</div>

<script>
function updateScore() {
    const laborType = document.getElementById('labor_type').value;
    const scoreInput = document.getElementById('labor_score');
    
    const scores = {
        '第一学年满16学时': 100,
        '第一学年达6-15学时': 80,
        '第一学年低于6学时': 0,
        '第二（三）学年满32学时': 100,
        '第二（三）学年未满32学时': 0,
        '入伍免修': 100
    };
    
    if (laborType in scores) {
        scoreInput.value = scores[laborType];
    } else {
        scoreInput.value = '';
    }
}

function approveScore(type, recordId) {
    if (confirm('确定要通过这条成绩记录吗？')) {
        const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        
        fetch(`/approve_${type}/${recordId}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': token
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert('审核通过成功！');
                window.location.reload();
            } else {
                alert('操作失败：' + data.message);
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('操作失败，请重试');
        });
    }
}

function rejectScore(type, recordId) {
    if (confirm('确定要驳回这条成绩记录吗？')) {
        const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        
        fetch(`/reject_${type}/${recordId}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': token
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert('驳回成功！');
                window.location.reload();
            } else {
                alert('操作失败：' + data.message);
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('操作失败，请重试');
        });
    }
}
</script>
{% endblock %} 